﻿using Hidistro.Entities.Commodities;
using Hidistro.SaleSystem.Catalog;
using Hidistro.UI.Common.Controls;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using ASPNET.WebControls;
using Hidistro.Core.Entities;
using Hidistro.Entities.Comments;
using Hidistro.Core;
using System.Web.UI.HtmlControls;
namespace Hidistro.UI.SaleSystem.Tags
{
    public class YB_Juseshangcheng_Detail : AscxTemplatedWebControl
    {
        private Repeater rptReview;
        private Repeater rptRank;
        private int ProductId = 1521;
        private Pager pager;
        private Image imgProduct;
        private Literal ltProduntNameImg;
        private Literal ltProduntName;
        private Literal ltProduntText;
        private Literal ltBrandName;
        private Literal ltCategory;
        private Literal ltProduntCode;
        private Literal ltProduntSale;
        private HtmlAnchor aImg;
        private HtmlAnchor aImglink;
        private HtmlAnchor aBuy;
        private Literal ltReviewsCount;
        private HtmlGenericControl mbx;

        protected override void AttachChildControls()
        {
            if (!string.IsNullOrEmpty(Context.Request["ProductId"]))
                ProductId = int.Parse(Context.Request["ProductId"].ToString());

            this.rptReview = (Repeater)this.FindControl("rptReview");
            this.rptRank = (Repeater)this.FindControl("rptRank");
            this.pager = (Pager)this.FindControl("pager");
            this.imgProduct = (Image)this.FindControl("imgProduct");
            this.ltProduntNameImg = (Literal)this.FindControl("ltProduntNameImg");
            this.ltProduntName = (Literal)this.FindControl("ltProduntName");
            this.ltProduntText = (Literal)this.FindControl("ltProduntText");
            this.ltBrandName = (Literal)this.FindControl("ltBrandName");
            this.ltCategory = (Literal)this.FindControl("ltCategory");
            this.ltProduntCode = (Literal)this.FindControl("ltProduntCode");
            this.ltProduntSale = (Literal)this.FindControl("ltProduntSale");
            this.aImg = (HtmlAnchor)this.FindControl("aImg");
            this.aImglink = (HtmlAnchor)this.FindControl("aImglink");
            this.aBuy = (HtmlAnchor)this.FindControl("aBuy");
            this.ltReviewsCount = (Literal)this.FindControl("ltReviewsCount");

            this.mbx = (HtmlGenericControl)this.FindControl("mbx");
            

            if (!this.Page.IsPostBack)
            {
                BindReviewList();
                BindRankList();
                ProductBrowseInfo pb = ProductBrowser.GetProductBrowseInfo(ProductId, null, null);
                if (pb != null)
                {
                    if (pb.Product != null)
                    {
                        imgProduct.ImageUrl = Globals.GetImgUrl(pb.Product.ThumbnailUrl310);
                        ltProduntNameImg.Text = pb.Product.ProductName;
                        ltProduntName.Text = pb.Product.ProductName;
                        ltProduntText.Text = pb.Product.ProductText;

                        StringBuilder builder = new StringBuilder();
                        if (pb.BrandName != null && pb.Product.BrandId != null)
                        {
                            builder.AppendFormat("<a class=\"detail-brand\" href=\"{0}\">{1}</a>&nbsp;", Globals.GetSiteUrls().SubBrandDetails((int)pb.Product.BrandId, null), pb.BrandName);
                        }

                        ltBrandName.Text = builder.ToString();
                        string paretcategory = "";
                        string paretcategoryName = "";
                        string curcategory = ProductBrowser.GetProductCategory(pb.Product.CategoryId).CategoryId.ToString() ;
                        string curcategoryName = ProductBrowser.GetProductCategory(pb.Product.CategoryId).Name;
                        try
                        {
                            paretcategory = ProductBrowser.GetProductCategory(ProductBrowser.GetProductCategory(pb.Product.CategoryId).ParentCategoryId.Value).CategoryId.ToString();
                            paretcategoryName = ProductBrowser.GetProductCategory(ProductBrowser.GetProductCategory(pb.Product.CategoryId).ParentCategoryId.Value).Name;
                        }
                        catch { }
                        ltCategory.Text = paretcategoryName + "&nbsp;" + curcategoryName;
                        ltProduntCode.Text = pb.Product.ProductCode;
                        ltProduntSale.Text = decimal.Parse(pb.Product.MarketPrice.ToString()).ToString("F0");
                        aImg.HRef =Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { ProductId, pb.Product.CategoryUrl });
                        aImglink.HRef = aImg.HRef;
                        aBuy.HRef = aImg.HRef;

                        mbx.InnerHtml += "<a href='/' target=\"_blank\">橘色商城</a>&nbsp;&gt;&nbsp;<a href=\"/juse/index.html\" target=\"_blank\">成人用品</a>&nbsp;&gt;&nbsp;<a href=\"/juse/category-"
                            + paretcategory + ".htm\">" + paretcategoryName + "</a>&nbsp;&gt;&nbsp;<a href=\"/juse/category/plist-" + curcategory + ".htm\">" + curcategoryName + "</a>&nbsp;&gt;&nbsp;" + pb.Product.ProductName + "</a>";
                    }
                }
            }


        }
        private void BindReviewList()
        {
            ProductReviewQuery reviewQuery = new ProductReviewQuery
            {
                PageIndex = this.pager.PageIndex,
                PageSize = this.pager.PageSize,
                productId = this.ProductId
            };
            DbQueryResult productReviews = ProductBrowser.GetProductReviews(reviewQuery);
            this.rptReview.DataSource = productReviews.Data;
            this.rptReview.DataBind();
            this.pager.TotalRecords = productReviews.TotalRecords;
            ltReviewsCount.Text = this.pager.TotalRecords.ToString();
        }
        private void BindRankList()
        {
            this.rptRank.DataSource = ProductBrowser.GetJsProductsForCategory(8, ProductId);
            this.rptRank.DataBind();
        }
        protected override void OnInit(EventArgs e)
        {
            if (this.SkinName == null)
            {
                this.SkinName = "/ascx/tags/YB_Juseshangcheng_Detail.ascx";
            }
            base.OnInit(e);
        }
    }
}
